<?php
/*
+----------------------------------------------------------------------+
| (c) Copyright Silkwires. 					                           |
| 	All Rights Reserved.                                               |
+----------------------------------------------------------------------+
| > Module: .........                                                  |
| > Author: (SW Team)  ........  					                   |
| >	Class description	: Defined data that is retrieved from database,| 
						  prepare some methods from base object        |
+----------------------------------------------------------------------+
*/

class Alert{
	
	private $db=null;
	private $config_site=null;
	
	function __construct(){
		$this->db=Zend_Registry::get('db');	
		$this->config_site=Zend_Registry::get('config_site');
	}
	
	function __destruct(){
		unset($this->db);
        unset($this->config_site);
	}
    
    function insert($arrData =array(), $arrData1=array()){    
        $id=0;
        if ($arrData){
            $arrData['price_from'] = $arrData['price_from'] * 1000000;
            $arrData['price_to'] = $arrData['price_to'] * 1000000;
            $email = $this->getAlert('type=0 and email=\''.$arrData['email'].'\'');
            if (count ($email)>0){
                $arrData['updated_time'] = date('Y-m-d H:i:s');
                $id = $this->db->update('alert_mail',$arrData, 'type=0 and email=\''.$arrData['email'].'\'');
               
            }else{
                $arrData['type']=0;
                $arrData['created_time'] = date('Y-m-d H:i:s');
                $arrData['updated_time'] = date('Y-m-d H:i:s');
                $this->db->insert('alert_mail',$arrData);
                $id = $this->db->lastInsertId('alert_mail');
            }
                
        }
		 
        if ($arrData1){    
            $arrData1['price_from'] = $arrData1['price_from'] * 1000000;
            $arrData1['price_to'] = $arrData1['price_to'] * 1000000;
             $email = $this->getAlert('type=1 and email=\''.$arrData1['email'].'\'');
         if (count ($email)>0){
            $arrData1['updated_time'] = date('Y-m-d H:i:s');
            $id = $this->db->update('alert_mail',$arrData1, 'type=1 and email=\''.$arrData1['email'].'\''); 
            
         }else{
            $arrData1['type']=1;
            $arrData1['created_time'] = date('Y-m-d H:i:s');
            $arrData1['updated_time'] = date('Y-m-d H:i:s');
            $this->db->insert('alert_mail',$arrData1); 
            $id = $this->db->lastInsertId('alert_mail');
         }
            
        }
  
        return $id;                       
	}
	function getAlert($strWhere='',$order='id', $sort='DESC', $offset=0, $size='all', $DISTINCT = false) {
        if($strWhere!='')
            $strWhere=' WHERE '.$strWhere;
        if ($DISTINCT)
            $SQL = 'SELECT DISTINCT * FROM alert_mail';
        else
            $SQL = 'SELECT * FROM alert_mail';               
		$SQL .= " $strWhere ".Common::getOrderBY($order,$sort,$offset,$size);
        
        //print($SQL);die;
		$rs=$this->db->fetchAll($SQL);
		return $rs;
	}
    function tracking_alert ($mid, $aid, $title, $content){
        $arrData = array ('mid'=>$mid,'aid'=>$aid,'title'=>$title, 'content'=>$content);
        $arrData['created_time'] = date('Y-m-d H:i:s');
        $arrData['updated_time'] = date('Y-m-d H:i:s');
        $this->db->insert('tracking_alert', $arrData);
    }
}    